import axios from "axios";
import { INodeInputSlot, INodeOutputSlot, LGraphNode } from "litegraph.js";

export class Http extends LGraphNode {
    public title = "Http请求";

    public inputs: INodeInputSlot[] = [
        {
            name: "url",
            type: "string",
            label: "地址",
            link: null,
        },
        {
            name: "请求方式",
            type: "string",
            label: "请求方式",
            link: null,
        },
    ];

    public outputs: INodeOutputSlot[] = [
        {
            links: null,
            name: "res",
            type: "object",
            label: "成功",
        },
        {
            links: null,
            name: "error",
            type: "object",
            label: "失败",
        },
    ];

    public properties = {
        url: "http://cim.cityworks.cn:20000/city-rainbow/rainbow/findRainbow?type=project",
        method: "GET",
    };

    constructor() {
        super();
        this.addWidget(
            "combo",
            "请求方式",
            "GET",
            (a: any, b, c) => {
                console.log(a, b, c);
            },
            { values: ["GET", "POST", "PUT", "DELETE"], property: "method" }
        );
        this.addWidget("text", "url", this.properties.url, undefined, { property: "url" });
    }

    public onStart() {
        axios({
            method: "get",
            url: this.properties.url,
            data: {
                firstName: "Fred",
                lastName: "Flintstone",
            },
        })
            .then(res => {
                console.log(res);
                this.setOutputData(0, res);
            })
            .catch(error => {
                console.error("error", error);
                this.setOutputData(1, error);
            });
        console.log(this);
    }
}
